﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using System.Data;
using CNVP.Framework;
using CNVP.Framework.Utils;
using CNVP.Framework.Helper;

namespace CNVP.JShop.Web.Admin
{
    public partial class ProductListClear : System.Web.UI.Page
    {
        BLL.CNVP_Products bll = new BLL.CNVP_Products();
        public int rplistCount = 0;
        public int PageIndex = 1;
        public string SearchType = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 106)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["Page"]) && Public.IsNumber(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            if (!string.IsNullOrEmpty(Request.QueryString["SearchType"]))
            {
                SearchType = Request.QueryString["SearchType"];
            }
            if (!IsPostBack)
            {
                ViewState["SearchType"] = "0";
                ProductClassIDBind();
                ddlProductTypeIDBind();
                StartLoad(PageIndex);
                #region 验证
                ddlProductTypeID.Attributes.Add("onchange", "ProTypeRefBranks();");
                btDelete.Attributes.Add("onclick", "if(ifCheck()){return confirm('确定删除该记录吗,删除后不能恢复?');}else{alert('请选择记录！');return false;};");
                btRestore.Attributes.Add("onclick", "if(ifCheck()){return confirm('确定还原该记录吗?');}else{alert('请选择记录！');return false;};");
                btDeleteAll.Attributes.Add("onclick", "return confirm('确定清空所有记录吗,删除后不能恢复?');");
                #endregion
            }
        }

        //商品分类
        private void ProductClassIDBind()
        {
            new BLL.BLLColumn().BuildAllDDList(this.ddlProductClassID,3);
        }
        //商品类型
        private void ddlProductTypeIDBind()
        {
            ddlProductTypeID.DataSource = new BLL.CNVP_ProductType().LoadAllUseDropDownList();
            ddlProductTypeID.DataTextField = "ProTypeName";
            ddlProductTypeID.DataValueField = "ID";
            ddlProductTypeID.DataBind();
        }

        /// <summary>
        /// PageNavigator1_PageChange 的摘要说明
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex)
        {
            SQLConditionInfo[] st = new SQLConditionInfo[12];
            st[0] = new SQLConditionInfo("@IsDelete", "1");
            st[1] = new SQLConditionInfo("@key", Public.FilterSql(txtkey.Text.Trim()));
            st[2] = new SQLConditionInfo("@ProductClassID", ddlProductClassID.SelectedValue);
            st[3] = new SQLConditionInfo("@ProductTypeID", ddlProductTypeID.SelectedValue);
            st[4] = new SQLConditionInfo("@key1", Public.FilterSql(txtkey1.Text.Trim()));
            st[6] = new SQLConditionInfo("@BrandID", Request.Form["listBranks"]);
            st[10] = new SQLConditionInfo("@ProductState", ddlProductState.SelectedValue);
            if (SearchType == "1")
            {
                string ProductClassID_Str = "";
                if (!string.IsNullOrEmpty(Request.QueryString["ProductClassID"]))
                {
                    ViewState["SearchType"] = "2";
                    ProductClassID_Str = Request.QueryString["ProductClassID"];
                    st[7] = new SQLConditionInfo("@ProductClassID", ProductClassID_Str);
                }
                string ProductTypeID_Str = "";
                if (!string.IsNullOrEmpty(Request.QueryString["ProductTypeID"]))
                {
                    ViewState["SearchType"] = "3";
                    ProductTypeID_Str = Request.QueryString["ProductTypeID"];
                    st[8] = new SQLConditionInfo("@ProductTypeID", ProductTypeID_Str);
                }
                string BrandID_Str = "";
                if (!string.IsNullOrEmpty(Request.QueryString["BrandID"]))
                {
                    ViewState["SearchType"] = "4";
                    BrandID_Str = Request.QueryString["BrandID"];
                    st[9] = new SQLConditionInfo("@BrandID", BrandID_Str);
                }
            }
            if (ViewState["SearchType"].ToString() == "5")
            {
                st[11] = new SQLConditionInfo("@ProductState", ViewState["ProductState"]);
            }
            st[5] = new SQLConditionInfo("@SearchType", ViewState["SearchType"]);
            int i, j;
            DataTable dt = null;
            int pageNum = 20;
            dt = bll.GetPage(this.GetType().Name, PageIndex, pageNum, out i, out j, st);
            if (dt != null)
            {
                rplistCount = dt.Rows.Count;//行数
                //----------------------------------------添加列------------------------------------------------
                dt.Columns.Add("oProductName", typeof(string));
                dt.Columns.Add("oProductClassID", typeof(string));
                dt.Columns.Add("oProductTypeID", typeof(string));
                dt.Columns.Add("oBrandID", typeof(string));
                dt.Columns.Add("oStockTotalNum", typeof(string));
                dt.Columns.Add("oProductState", typeof(string));
                //----------------------------------------添加列结束--------------------------------------------
                for (int k = 0; dt.Rows.Count > k; k++)
                {
                    dt.Rows[k]["oProductName"] = BasePage.CutString(dt.Rows[k]["ProductName"].ToString(), 30);
                    string _ColumnName = "";
                    var model_Column = new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(Convert.ToInt32(dt.Rows[k]["ProductClassID"]));
                    if (model_Column != null)
                    {
                        _ColumnName = model_Column.ColumnName;
                    }
                    dt.Rows[k]["oProductClassID"] = _ColumnName;
                    string _ProTypeName = "";
                    var model_ProductType = new BLL.CNVP_ProductType().GetSingle_CNVP_ProductTypeById(Convert.ToInt32(dt.Rows[k]["ProductTypeID"]));
                    if (model_ProductType != null)
                    {
                        _ProTypeName = model_ProductType.ProTypeName;
                    }
                    dt.Rows[k]["oProductTypeID"] = _ProTypeName;
                    string oBrandID = "";
                    try
                    {
                        oBrandID = new BLL.CNVP_Branks().GetSingle_CNVP_BranksById(Convert.ToInt32(dt.Rows[k]["BrandID"])).BrankName;
                    }
                    catch { oBrandID = ""; }
                    dt.Rows[k]["oBrandID"] = oBrandID;
                    int StockNum = 0;
                    var ProductsDetail_List = new BLL.CNVP_ProductsDetail().GetAll_CNVP_ProductsDetail(Convert.ToInt32(dt.Rows[k]["ID"]));
                    for (int a = 0; a < ProductsDetail_List.Count; a++)
                    {
                        StockNum += ProductsDetail_List[a].StockNum;
                    }
                    dt.Rows[k]["oStockTotalNum"] = StockNum;
                    if (dt.Rows[k]["ProductState"].ToString() == "1")
                    {
                        dt.Rows[k]["oProductState"] = "上架";
                    }
                    else
                    {
                        dt.Rows[k]["oProductState"] = "<font color=\"red\">下架</font>";
                    }
                }
            }
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }
        protected void btDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.rpList.Items.Count; i++)
            {
                CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                if (cbCheck.Checked == true)
                {
                    int code = int.Parse(cbCheck.ToolTip);
                    var ProductsDetail_List = new BLL.CNVP_ProductsDetail().GetAll_CNVP_ProductsDetail(code);
                    if (ProductsDetail_List.Count > 0)
                    {
                        foreach (Model.CNVP_ProductsDetail p in ProductsDetail_List)
                        {
                            new BLL.CNVP_ProductsDetail().Delete_CNVP_ProductsDetail(p.ID);//删除详细产品
                            var ProductsPic_List = new BLL.CNVP_ProductsPic().GetAll_CNVP_ProductsPic(code);//删除产品图片
                            foreach (Model.CNVP_ProductsPic pic in ProductsPic_List)
                            {
                                new BLL.CNVP_ProductsPic().Delete_CNVP_ProductsPic(pic.ID);
                            }
                            var ProExtendeProperty_List = new BLL.CNVP_ProExtendeProperty().GetAll_CNVP_ProExtendeProperty(code);//删除扩展属性
                            foreach (Model.CNVP_ProExtendeProperty Property in ProExtendeProperty_List)
                            {
                                new BLL.CNVP_ProExtendeProperty().Delete_CNVP_ProExtendeProperty(Property.ID);
                            }
                            var ProPropertyDescripttion_List = new BLL.CNVP_ProPropertyDescripttion().GetAll_CNVP_ProPropertyDescripttion(code);//删除属性信息
                            foreach (Model.CNVP_ProPropertyDescripttion Descripttion in ProPropertyDescripttion_List)
                            {
                                new BLL.CNVP_ProPropertyDescripttion().Delete_CNVP_ProPropertyDescripttion(Descripttion.ID);
                            }
                            try
                            {
                                new BLL.CNVP_SEOInfo().Delete_CNVP_SEOInfo(new BLL.CNVP_Products().GetSingle_CNVP_ProductsById(code).SeoInfoID);//删除SEO优化
                            }
                            catch { }
                            //删除文章中相关产品ID:感觉效率低
                            var NewsInfo_list = new BLL.CNVP_NewsInfo().GetAll_CNVP_NewsInfo();
                            foreach (Model.CNVP_NewsInfo n in NewsInfo_list)
                            {
                                string ProductsID = "";
                                string[] nHotLink = n.HotLink.Split(Convert.ToChar(","));
                                for (int k = 0; k < nHotLink.Length; k++)
                                {
                                    string HotLinkProductsID = nHotLink[k];
                                    if (HotLinkProductsID == "")
                                    {
                                        HotLinkProductsID = "0";
                                    }
                                    if (code == int.Parse(HotLinkProductsID))
                                    {
                                        continue;
                                    }
                                    ProductsID += nHotLink[k] + ",";
                                }
                                if (ProductsID != "")
                                {
                                    ProductsID = ProductsID.Substring(0, ProductsID.Length - 1);
                                }
                                new BLL.CNVP_NewsInfo().Update_CNVP_NewsInfoHotLink(n.ID, ProductsID);
                            }
                            bll.Delete_CNVP_Products(code);
                        }
                    }
                    else
                    {
                        var ProductsPic_List = new BLL.CNVP_ProductsPic().GetAll_CNVP_ProductsPic(code);//删除产品图片
                        foreach (Model.CNVP_ProductsPic pic in ProductsPic_List)
                        {
                            new BLL.CNVP_ProductsPic().Delete_CNVP_ProductsPic(pic.ID);
                        }
                        var ProExtendeProperty_List = new BLL.CNVP_ProExtendeProperty().GetAll_CNVP_ProExtendeProperty(code);//删除扩展属性
                        foreach (Model.CNVP_ProExtendeProperty Property in ProExtendeProperty_List)
                        {
                            new BLL.CNVP_ProExtendeProperty().Delete_CNVP_ProExtendeProperty(Property.ID);
                        }
                        var ProPropertyDescripttion_List = new BLL.CNVP_ProPropertyDescripttion().GetAll_CNVP_ProPropertyDescripttion(code);//删除属性信息
                        foreach (Model.CNVP_ProPropertyDescripttion Descripttion in ProPropertyDescripttion_List)
                        {
                            new BLL.CNVP_ProPropertyDescripttion().Delete_CNVP_ProPropertyDescripttion(Descripttion.ID);
                        }
                        try
                        {
                            new BLL.CNVP_SEOInfo().Delete_CNVP_SEOInfo(new BLL.CNVP_Products().GetSingle_CNVP_ProductsById(code).SeoInfoID);//删除SEO优化
                        }
                        catch { }
                        //删除文章中相关产品ID:感觉效率低
                        var NewsInfo_list = new BLL.CNVP_NewsInfo().GetAll_CNVP_NewsInfo();
                        foreach (Model.CNVP_NewsInfo n in NewsInfo_list)
                        {
                            string ProductsID = "";
                            string[] nHotLink = n.HotLink.Split(Convert.ToChar(","));
                            for (int k = 0; k < nHotLink.Length; k++)
                            {
                                string HotLinkProductsID = nHotLink[k];
                                if (HotLinkProductsID == "")
                                {
                                    HotLinkProductsID = "0";
                                }
                                if (code == int.Parse(HotLinkProductsID))
                                {
                                    continue;
                                }
                                ProductsID += nHotLink[k] + ",";
                            }
                            if (ProductsID != "")
                            {
                                ProductsID = ProductsID.Substring(0, ProductsID.Length - 1);
                            }
                            new BLL.CNVP_NewsInfo().Update_CNVP_NewsInfoHotLink(n.ID, ProductsID);
                        }
                        bll.Delete_CNVP_Products(code);
                    }
                }
            }
            BasePage.MessageBox("删除操作成功！", "ProductListClear.aspx");
        }

        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {
            ViewState["SearchType"] = "0";
            StartLoad(PageIndex);
        }

        protected void btHighSearch_Click(object sender, EventArgs e)
        {
            ViewState["SearchType"] = "1";
            StartLoad(PageIndex);

        }

        protected void btRestore_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.rpList.Items.Count; i++)
            {
                CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                if (cbCheck.Checked == true)
                {
                    int code = int.Parse(cbCheck.ToolTip);
                    Model.CNVP_Products model = new Model.CNVP_Products();//还原
                    model.ID = code;
                    model.IsDelete = 0;
                    bll.Update_CNVP_ProductsByIsDelete(model);
                }
            }
            StartLoad(PageIndex);
        }

        protected void btDeleteAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.rpList.Items.Count; i++)
            {

                CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                int code = int.Parse(cbCheck.ToolTip);
                var ProductsDetail_List = new BLL.CNVP_ProductsDetail().GetAll_CNVP_ProductsDetail(code);
                if (ProductsDetail_List.Count > 0)
                {
                    foreach (Model.CNVP_ProductsDetail p in ProductsDetail_List)
                    {
                        new BLL.CNVP_ProductsDetail().Delete_CNVP_ProductsDetail(p.ID);//删除详细产品
                        var ProductsPic_List = new BLL.CNVP_ProductsPic().GetAll_CNVP_ProductsPic(code);//删除产品图片
                        foreach (Model.CNVP_ProductsPic pic in ProductsPic_List)
                        {
                            new BLL.CNVP_ProductsPic().Delete_CNVP_ProductsPic(pic.ID);
                        }
                        var ProExtendeProperty_List = new BLL.CNVP_ProExtendeProperty().GetAll_CNVP_ProExtendeProperty(code);//删除扩展属性
                        foreach (Model.CNVP_ProExtendeProperty Property in ProExtendeProperty_List)
                        {
                            new BLL.CNVP_ProExtendeProperty().Delete_CNVP_ProExtendeProperty(Property.ID);
                        }
                        var ProPropertyDescripttion_List = new BLL.CNVP_ProPropertyDescripttion().GetAll_CNVP_ProPropertyDescripttion(code);//删除属性信息
                        foreach (Model.CNVP_ProPropertyDescripttion Descripttion in ProPropertyDescripttion_List)
                        {
                            new BLL.CNVP_ProPropertyDescripttion().Delete_CNVP_ProPropertyDescripttion(Descripttion.ID);
                        }
                        try
                        {
                            new BLL.CNVP_SEOInfo().Delete_CNVP_SEOInfo(new BLL.CNVP_Products().GetSingle_CNVP_ProductsById(code).SeoInfoID);//删除SEO优化
                        }
                        catch { }
                        //删除文章中相关产品ID:感觉效率低
                        var NewsInfo_list = new BLL.CNVP_NewsInfo().GetAll_CNVP_NewsInfo();
                        foreach (Model.CNVP_NewsInfo n in NewsInfo_list)
                        {
                            string ProductsID = "";
                            string[] nHotLink = n.HotLink.Split(Convert.ToChar(","));
                            for (int k = 0; k < nHotLink.Length; k++)
                            {
                                string HotLinkProductsID = nHotLink[k];
                                if (HotLinkProductsID == "")
                                {
                                    HotLinkProductsID = "0";
                                }
                                if (code == int.Parse(HotLinkProductsID))
                                {
                                    continue;
                                }
                                ProductsID += nHotLink[k] + ",";
                            }
                            if (ProductsID != "")
                            {
                                ProductsID = ProductsID.Substring(0, ProductsID.Length - 1);
                            }
                            new BLL.CNVP_NewsInfo().Update_CNVP_NewsInfoHotLink(n.ID, ProductsID);
                        }
                        bll.Delete_CNVP_Products(code);
                    }
                }
                else
                {
                    var ProductsPic_List = new BLL.CNVP_ProductsPic().GetAll_CNVP_ProductsPic(code);//删除产品图片
                    foreach (Model.CNVP_ProductsPic pic in ProductsPic_List)
                    {
                        new BLL.CNVP_ProductsPic().Delete_CNVP_ProductsPic(pic.ID);
                    }
                    var ProExtendeProperty_List = new BLL.CNVP_ProExtendeProperty().GetAll_CNVP_ProExtendeProperty(code);//删除扩展属性
                    foreach (Model.CNVP_ProExtendeProperty Property in ProExtendeProperty_List)
                    {
                        new BLL.CNVP_ProExtendeProperty().Delete_CNVP_ProExtendeProperty(Property.ID);
                    }
                    var ProPropertyDescripttion_List = new BLL.CNVP_ProPropertyDescripttion().GetAll_CNVP_ProPropertyDescripttion(code);//删除属性信息
                    foreach (Model.CNVP_ProPropertyDescripttion Descripttion in ProPropertyDescripttion_List)
                    {
                        new BLL.CNVP_ProPropertyDescripttion().Delete_CNVP_ProPropertyDescripttion(Descripttion.ID);
                    }
                    try
                    {
                        new BLL.CNVP_SEOInfo().Delete_CNVP_SEOInfo(new BLL.CNVP_Products().GetSingle_CNVP_ProductsById(code).SeoInfoID);//删除SEO优化
                    }
                    catch { }
                    //删除文章中相关产品ID:感觉效率低
                    var NewsInfo_list = new BLL.CNVP_NewsInfo().GetAll_CNVP_NewsInfo();
                    foreach (Model.CNVP_NewsInfo n in NewsInfo_list)
                    {
                        string ProductsID = "";
                        string[] nHotLink = n.HotLink.Split(Convert.ToChar(","));
                        for (int k = 0; k < nHotLink.Length; k++)
                        {
                            string HotLinkProductsID = nHotLink[k];
                            if (HotLinkProductsID == "")
                            {
                                HotLinkProductsID = "0";
                            }
                            if (code == int.Parse(HotLinkProductsID))
                            {
                                continue;
                            }
                            ProductsID += nHotLink[k] + ",";
                        }
                        if (ProductsID != "")
                        {
                            ProductsID = ProductsID.Substring(0, ProductsID.Length - 1);
                        }
                        new BLL.CNVP_NewsInfo().Update_CNVP_NewsInfoHotLink(n.ID, ProductsID);
                    }
                    bll.Delete_CNVP_Products(code);
                }
            }
            BasePage.MessageBox("清空操作成功！", "ProductListClear.aspx");
        }

        
    }
}
